/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_ora_service_list_add.c                           */
/*  NOTE      =                                                    */
/*  DATE      = 2016/03/17 by songbb                               */
/*******************************************************************/

#include <CUnit/Basic.h>

#include "frcomm.h"

#include "../../../../src/module/oracle/config.h"
#include "../../test_ora_comm.h"
#include <oracle/comm/ora_comm.h>
#include <apmtest_comm.h>
#include <frservice_mng.h>
#include <frservice_comm.h>

extern ApmContext g_st_apm_context;
extern void *g_p_apm_context;

extern void __ora_destroy_session(L7session_ora **pp_st_session);

static int __suite_init(void)
{
    frservice_mng_init();
    frservice_setRef(g_st_apm_context.p_st_service_ref);
    g_p_apm_context = &g_st_apm_context;
    return 0;
}

static int __suite_cleanup(void)
{
    frservice_mng_destory();
    return 0;
}

/**
 * 正常测试
 */
static void __case1(void)
{
    L7session_ora *p_st_ora_sess = test_ora_session_create();
    CU_ASSERT_PTR_NOT_NULL(p_st_ora_sess);

    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, NULL, 0, TEST_ORA_SRC_IP, TEST_ORA_DST_IP, TEST_ORA_SRC_PORT, TEST_ORA_DST_PORT, st_timestamp), FR_SUCC);

    p_st_ora_sess->s_version_number = 9000;
    
    ora_service_list_add(p_st_ora_sess, p_st_packet);

    CU_ASSERT_EQUAL(frservice_get(g_p_apm_context, MODULE_L7PROTO, p_st_packet->src_ip, p_st_packet->src_port, NULL, 0, NULL), FR_FAIL);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);

    __ora_destroy_session(&p_st_ora_sess);
    CU_ASSERT_PTR_NULL(p_st_ora_sess);
}

/**
 * 异常测试
 */
static void __case2(void)
{
    L7session_ora *p_st_ora_sess = test_ora_session_create();
    CU_ASSERT_PTR_NOT_NULL(p_st_ora_sess);

    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, NULL, 0, TEST_ORA_SRC_IP, TEST_ORA_DST_IP, TEST_ORA_SRC_PORT, TEST_ORA_DST_PORT, st_timestamp), FR_SUCC);

    p_st_ora_sess->s_version_number = 10000;

    ora_service_list_add(p_st_ora_sess, p_st_packet);

    CU_ASSERT_EQUAL(frservice_get(g_p_apm_context, MODULE_L7PROTO, p_st_packet->src_ip, (u_short) 1400, NULL, 0, NULL), FR_FAIL);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);

    __ora_destroy_session(&p_st_ora_sess);
    CU_ASSERT_PTR_NULL(p_st_ora_sess);
}

/**
 * 异常测试
 */
static void __case3(void)
{
    L7session_ora *p_st_ora_sess = test_ora_session_create();
    CU_ASSERT_PTR_NOT_NULL(p_st_ora_sess);

    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, NULL, 0, TEST_ORA_SRC_IP, TEST_ORA_DST_IP, TEST_ORA_SRC_PORT, TEST_ORA_DST_PORT, st_timestamp), FR_SUCC);

    p_st_ora_sess->s_version_number = 10000;

    ora_service_list_add(p_st_ora_sess, p_st_packet);

    CU_ASSERT_EQUAL(frservice_get(g_p_apm_context, MODULE_L7PROTO, p_st_packet->src_ip, p_st_packet->src_port, NULL, 0, NULL), FR_SUCC);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);

    __ora_destroy_session(&p_st_ora_sess);
    CU_ASSERT_PTR_NULL(p_st_ora_sess);
}

int add_suite_ora_service_list_add(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" ora_service_list_add()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL ||
        CU_add_test(pSuite, "case2", __case2) == NULL ||
        CU_add_test(pSuite, "case3", __case3) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
